import { MessageBox } from 'element-ui'
import storage from './storage'


export const isDel =  (name, title='提示') =>{
  return MessageBox.confirm(`此操作将删除${name}, 是否继续?`, title, {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
      return true;
  }).catch(err=>{
    return false;
  })
}


export const isConfirm =  (message, title='提示') =>{
  return MessageBox.confirm(message, title, {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
      return true;
  }).catch(err=>{
    return false;
  })
}

export const download =  (data, fileName) =>{
  let blob = new Blob([ JSON.stringify( data, null, 2 ) ])
  // 生成a标签
  let downloadElement = document.createElement('a')
  // 创建下载的链接
  let href = window.URL.createObjectURL(blob)
  // a的下载链接
  downloadElement.href = href
  // 设置下载后文件名
  downloadElement.download = fileName
  // 添加a标签
  document.body.appendChild(downloadElement)
  // 点击下载
  downloadElement.click()
  // 下载完成移除元素
  document.body.removeChild(downloadElement)
  // 释放掉blob对象
  window.URL.revokeObjectURL(href)
}

export const getBaseURL = () => {
  const baseURL = storage.local.get('baseURL');
  return 'http://' + baseURL.replace('http://', '') ;
}